package com.demoReflect;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.lang.reflect.Field;

public class ObjectFrame {
    public static void saveObject(Object obj) throws  FileNotFoundException, IllegalArgumentException, IllegalAccessException{
        File file = new File("data1.txt");
        PrintStream ps = new PrintStream(new FileOutputStream(file));
        Class c=obj.getClass();
        String className = c.getName();
        ps.println("------------------------"+className+"----------------------");

        Field[] fields = c.getDeclaredFields();
        for (Field field : fields) {
            String name=field.getName();

            field.setAccessible(true);
            String value=field.get(obj)+ " ";
            ps.println(name + "=" + value);
        }
        ps.close();

    }
}
